package com.lc.pianostudent.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.conn.PostFeedBack;
import com.lc.pianostudent.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2018/6/25.
 */

public class FeedBackActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(R.id.content_edit) private EditText content_edit;
    @BoundView(R.id.tele) private EditText tele;
    @BoundView(value = R.id.send_feed_back,isClick = true) private LinearLayout send_feed_back;
    private String phone = "";
    @BoundView(R.id.total_count) private TextView total_count;

    //意见反馈
    public PostFeedBack postFeedBack = new PostFeedBack(new AsyCallBack<PostFeedBack.PostFeedBackInfo>() {

        @Override
        public void onSuccess(String toast, int type, Object o, PostFeedBack.PostFeedBackInfo postFeedBackInfo) throws Exception {

            UtilToast.show(toast);
            finish();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);


        setBackTrue();

        initView();
    }

    private void initView(){
        setTitleName("意见反馈");
        content_edit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(200)});
        content_edit.addTextChangedListener(passwordListener());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.send_feed_back:
                if (TextUtils.isEmpty(content_edit.getText().toString().trim())) {
                    UtilToast.show("请输入反馈内容");
                }else {
                    if (!TextUtils.isEmpty(tele.getText().toString().trim())) {
                        if (!Validator.isMobile(tele.getText().toString().trim())) {
                            UtilToast.show("手机号格式错误");
                        }else {
                            phone = tele.getText().toString();
                            postFeedBack.userid = BaseApplication.BasePreferences.readUID();
                            postFeedBack.content = content_edit.getText().toString();
                            postFeedBack.phone = phone;
                            postFeedBack.execute();
                        }

                    }else {
                        phone = tele.getText().toString();
                        postFeedBack.userid = BaseApplication.BasePreferences.readUID();
                        postFeedBack.content = content_edit.getText().toString();
                        postFeedBack.phone = phone;
                        postFeedBack.execute();
                    }

                }
                break;
        }
    }

    private TextWatcher passwordListener() {
        return new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                int length = s.length();
//                Log.e("---len",length+"===jy");
                total_count.setText(length+"");
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
    }
}
